package com.dawnling.greendaodemo.converter;

import com.dawnling.greendaodemo.bean.UserChildBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.greendao.converter.PropertyConverter;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * GreenDao：
 * List<UserChildBean> 对象数组转换器
 */
public class ObjectListConverter implements PropertyConverter<List<UserChildBean>,String> {

    private final Gson mGson =  new Gson();

    @Override
    public List<UserChildBean> convertToEntityProperty(String databaseValue) {
        Type type = new TypeToken<ArrayList<UserChildBean>>() {}.getType();
        ArrayList<UserChildBean> itemList= mGson.fromJson(databaseValue, type);
        return itemList;
    }

    @Override
    public String convertToDatabaseValue(List<UserChildBean> entityProperty) {
        String dbString = mGson.toJson(entityProperty);
        return dbString;
    }
}